#Author: Tanyong
# *-* coding:utf8 *-*
# @time       : 18-7-16 下午8:08
# @Author     : Tanyong
# @Site       : 
# @File       : views.py
# @Software   : PyCharm
import time
from flask import render_template, flash, redirect, url_for

from info.Models import Info, db
from .forms import InfoForm
from . import admin
from wtforms import StringField, SubmitField
from wtforms.validators import DataRequired



@admin.route('/', methods=['GET'])
def index():
    """查询所有"""
    all_Data_Info = Info.query.all()
    return render_template('admin/center.html', alldatainfo=all_Data_Info)


@admin.route('/info/add', methods=['GET', 'POST'])
def info_add():
    """添加股票信息"""
    form = InfoForm()
    if form.validate_on_submit():
        data = form.data
        infoadd = Info(
            code=data['code'],
            short=data['short'],
            chg=data['chg'],
            turnover=data['turnover'],
            price=data['price'],
            highs=data['highs'],
        )
        db.session.add(infoadd)
        db.session.commit()
        flash("添加股票信息成功！", "ok")
        return redirect(url_for('admin.index'))
    return render_template('admin/infoAdd.html', form=form)


@admin.route('/info/del/<code>')
def info_del(code):
    """删除股票"""
    info = Info.query.filter(Info.code==code).first()
    if not info:
        flash("没有这个股票(%s)" %code)
    else:
        try:
            db.session.delete(info)
            db.session.commit()
        except Exception as e:
            db.session.rollback()
            print(e)
            flash("删除股票失败！")

    return redirect(url_for('admin.index'))


@admin.route('/info/update/<code>', methods=['GET', 'POST'])
def info_update(code):
    """修改信息"""
    info = Info.query.filter(Info.code == code).first()

    class InfoUpdateForm(InfoForm):
        code = StringField(label="股票代码", validators=[DataRequired("请输入股票代码")], default=info.code)
        short = StringField(label="股票简称", validators=[DataRequired("请输入股票简称")], default=info.short)
        chg = StringField(label="涨跌幅", validators=[DataRequired("请输入股票涨跌幅")], default=info.chg)
        turnover = StringField(label="股票换手率", validators=[DataRequired("请输入股票换手率")], default=info.turnover)
        price = StringField(label="股票价格", validators=[DataRequired("请输入股票价格")], default=info.price)
        highs = StringField(label="股票最高点", validators=[DataRequired("请输入股票最高点")], default=info.highs)
        submit = SubmitField('保存', render_kw={"class": "btn btn-primary"})

    form = InfoUpdateForm()
    # form.code = StringField(label="股票代码", validators=[DataRequired("请输入股票代码")], default=info.code)
    # form.short = StringField(label="股票简称", validators=[DataRequired("请输入股票简称")], default=info.short)
    # form.chg = StringField(label="涨跌幅", validators=[DataRequired("请输入股票涨跌幅")], default=info.chg)
    # form.turnover = StringField(label="股票换手率", validators=[DataRequired("请输入股票换手率")], default=info.turnover)
    # form.price = StringField(label="股票价格", validators=[DataRequired("请输入股票价格")], default=info.price)
    # form.highs = StringField(label="股票最高点", validators=[DataRequired("请输入股票最高点")], default=info.highs)
    # form.submit = SubmitField('保存', render_kw={"class": "btn btn-primary"})
    # 这样更改无效， 会导致系统识别不了 Field类型

    if form.validate_on_submit():
        data = form.data
        info.code=data['code']
        info.short=data['short']
        info.chg=data['chg']
        info.turnover=data['turnover']
        info.price=data['price']
        info.highs=data['highs']

        db.session.commit()
        flash("修改股票信息成功！", "ok")
        return redirect(url_for('admin.index'))
    return render_template('admin/infoUpdate.html', form=form)
